package com.itheima.algorithm.leetcode;

/**
 * @author: TylerZhong
 * @description: 找到中间节点，（偶数以右边中间为基准）
 */
public class E08Leetcode876 {

    // 用快慢指针解决
    public ListNode middleNode(ListNode head) {
        ListNode p1 = head;
        ListNode p2 = head;
        while (p2 != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next;
            p2 = p2.next;
        }
        return p1;
    }

    public static void main(String[] args) {
        ListNode head1 = ListNode.of(1, 2, 3, 4, 5);
        ListNode listNode = new E08Leetcode876().middleNode(head1);
        System.out.println(listNode);

        ListNode head2 = ListNode.of(1, 2, 3, 4, 5, 6);
        ListNode listNode1 = new E08Leetcode876().middleNode(head2);
        System.out.println(listNode1);

    }
}
